//
//  TaxesResultViewController.m
//  Rong360
//
//  Created by tianliwei on 9/2/14.
//  Copyright (c) 2014 tianliwei. All rights reserved.
//

#import "TaxesResultViewController.h"
#import "TaxesResultCell.h"
#import "WebViewController.h"
#import "TaxesHistoryViewController.h"
@interface TaxesResultViewController ()
@property (nonatomic, strong) NSArray *titles;
@property (nonatomic, strong) NSArray *subTitles;
@end

static NSString *CellIdentifier = @"Cell";

@implementation TaxesResultViewController

- (id)initWithTaxesCal:(TaxesCalculator *)taxesCal
{
    self = [super init];
    if (self) {
        self.taxesCal = taxesCal;
    }
    return self;
}

- (void)history{
    self.hidesBottomBarWhenPushed = YES;
    TaxesHistoryViewController *taxesHistoryVC = [[TaxesHistoryViewController alloc]initWithHousehandType:self.taxesCal.houseHandType];
    [self.navigationController pushViewController:taxesHistoryVC animated:YES];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"计算结果";
    self.titles = @[@"合计",@"契税",@"营业税及附加",@"个人所得税",@"印花税",@"综合地价款",@"中介费"];
    NSInteger rowHeight = 53;

    self.view.backgroundColor = [UIColor whiteColor];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, UI_NAVIFRAME_HEIGHT)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.rowHeight = rowHeight;
    self.tableView.scrollEnabled = YES;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:@"TaxesResultCell" bundle:nil] forCellReuseIdentifier:CellIdentifier];
    [self.view addSubview:self.tableView];
    [self initTableFooterView];

    UIButton *historyButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [historyButton setBackgroundColor:[UIColor clearColor]];
    [historyButton setTitle:@"历史" forState:UIControlStateNormal];
    [historyButton addTarget:self action:@selector(history) forControlEvents:UIControlEventTouchUpInside];
    historyButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    historyButton.frame = CGRectMake(0.0f, 0.0f, 40.0f, 25.0f);
    UIBarButtonItem *historyButtonItem = [[UIBarButtonItem alloc] initWithCustomView:historyButton];
    self.navigationItem.rightBarButtonItem = historyButtonItem;
}

- (void)initTableFooterView{
    UIView *tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, UI_SCREEN_WIDTH, 130)];
    UILabel *label = [UILabel labelWithFrame:CGRectMake(15, 10, 300, 15) title:@"以上结果仅供参考，请以当地实际情况为准" fontColor:[UIColor lightGrayColor] fontSize:10];
    
    UIButton *taxesDetailBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [taxesDetailBtn setFrame:CGRectMake(15, 40, 290, 35)];
    [taxesDetailBtn setTitle:@"各项税费详细说明" forState:UIControlStateNormal];
    [taxesDetailBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 6, 0, 0)];
    [taxesDetailBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [taxesDetailBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [taxesDetailBtn setBackgroundImage:[UIImage imageNamed:@"btn_grey_normal"] forState:UIControlStateNormal];
    [taxesDetailBtn setBackgroundImage:[UIImage imageNamed:@"btn_grey_focus"] forState:UIControlStateHighlighted];
    [taxesDetailBtn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
    [taxesDetailBtn addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *reCal = [UIButton buttonWithTag:1 frame:CGRectMake(15, 85, 135, 35) normalImage:[UIImage imageNamed:@"btn_qd_normal"] highlightedImage:[UIImage imageNamed:@"btn_qd_focus"] target:self action:@selector(buttonClicked:)];
    [reCal setTitle:@"重新计算" forState:UIControlStateNormal];
    [reCal.titleLabel setFont:[UIFont systemFontOfSize:14]];
    UIButton *sendResult = [UIButton buttonWithTag:2 frame:CGRectMake(170, 85, 135, 35) normalImage:[UIImage imageNamed:@"btn_tg_normal"] highlightedImage:[UIImage imageNamed:@"btn_tg_focus"] target:self action:@selector(buttonClicked:)];
    [sendResult setTitle:@"发送结果" forState:UIControlStateNormal];
    [sendResult.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [tableFooterView addSubview:taxesDetailBtn];
    [tableFooterView addSubview:label];
    [tableFooterView addSubview:reCal];
    [tableFooterView addSubview:sendResult];
    self.tableView.tableFooterView = tableFooterView;
}

- (void)buttonClicked:(UIButton *)sender{
    switch (sender.tag) {
        case 0:{
            self.hidesBottomBarWhenPushed = YES;
            WebViewController *webVC = [[WebViewController alloc]initWithUrl:MFSFUrl title:@"各项税费详细说明" desc:MFSFDesc];
            webVC.type = HTML5ShareUrl;
            [self.navigationController pushViewController:webVC animated:YES];
            break;
        }
        case 1:{
            [self.navigationController popViewControllerAnimated:YES];
            break;
        }
        case 2:{
            NSString *houseType;
            if (self.taxesCal.houseHandType == HouseSecondHand) {
                houseType = @"二手房";
            }else if (self.taxesCal.houseHandType == HouseNew){
                houseType = @"新房";
            }
            NSString *price;
            if (self.taxesCal.houseHandType == HouseSecondHand) {
                price = self.taxesCal.totalPrice_second;
            }else if (self.taxesCal.houseHandType == HouseNew){
                price = self.taxesCal.totalPrice_new;
            }
            NSString *shareText = [NSString stringWithFormat:@"我在#融360买房助手#中测得买%@万的%@需要缴纳%@元的税费。这个应用真的很强大，用了之后，基本不怕被中介忽悠了,你也来试试吧",price,houseType,self.taxesCal.results[0]];
            
            [UMSocialData defaultData].extConfig.wxMessageType = UMSocialWXMessageTypeWeb;
            [UMSocialWechatHandler setWXAppId:kWXAppID url:kRong360Host];
            [UMSocialConfig setQQAppId:kTencentID url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
//            [UMSocialConfig setShareQzoneWithQQSDK:YES url:kRong360Host importClasses:@[[QQApiInterface class],[TencentOAuth class]]];
            [UMSocialSnsService presentSnsIconSheetView:self
                                                 appKey:kUMSocialKey
                                              shareText:shareText
                                             shareImage:[UIImage imageNamed:kShareIcon]
                                        shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToQQ,nil]
                                               delegate:nil];
            break;
        }
        default:
            break;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    // Return the number of rows in the section.
    return self.titles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    TaxesResultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell.mainLabel.text = self.titles[indexPath.row];
    if (indexPath.row == 0) {
        cell.sub1Label.textColor = [UIColor redColor];
        if (self.taxesCal.houseHandType == HouseSecondHand) {
            if (![[NSUserDefaults standardUserDefaults]objectForKey:@"LastHouseSecondHand"]) {
                NSString *lastResult = [NSString stringWithFormat:@"上次结果："];
                cell.sub2Label.text = lastResult;
                [[NSUserDefaults standardUserDefaults] setObject:self.taxesCal.heji forKey:@"LastHouseSecondHand"];
            }else{
                NSString *lastResult = [NSString stringWithFormat:@"上次结果：%@元",[[NSUserDefaults standardUserDefaults]objectForKey:@"LastHouseSecondHand"]];
                cell.sub2Label.text = lastResult;
                [[NSUserDefaults standardUserDefaults] setObject:self.taxesCal.heji forKey:@"LastHouseSecondHand"];
            }
            
        }else if (self.taxesCal.houseHandType == HouseNew){
            
            if (![[NSUserDefaults standardUserDefaults]objectForKey:@"LastHouseNew"]) {
                NSString *lastResult = [NSString stringWithFormat:@"上次结果："];
                cell.sub2Label.text = lastResult;
                [[NSUserDefaults standardUserDefaults]setObject:self.taxesCal.heji forKey:@"LastHouseNew"];
            }else{
                NSString *lastResult = [NSString stringWithFormat:@"上次结果：%@元",[[NSUserDefaults standardUserDefaults]objectForKey:@"LastHouseNew"]];
                cell.sub2Label.text = lastResult;
                [[NSUserDefaults standardUserDefaults]setObject:self.taxesCal.heji forKey:@"LastHouseNew"];
            }
            
//            NSString *lastResult = [NSString stringWithFormat:@"上次结果：%@元",self.taxesCal.heji];
//            cell.sub2Label.text = lastResult;
        }
        
    }else if (indexPath.row == 1) {
        
        NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",self.taxesCal.qishui_cur];
        cell.sub2Label.text = sub2Str;
        
    }else if (indexPath.row == 2) {
        if ([self.taxesCal.yingyefujia floatValue] > 0) {
            NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",[Config shareConfig].yingyeshui];
            cell.sub2Label.text = sub2Str;
        }else{
            NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",@"0"];
            cell.sub2Label.text = sub2Str;
        }
        
    }else if (indexPath.row == 3) {
        NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",self.taxesCal.geren_cur];
        cell.sub2Label.text = sub2Str;
    }else if (indexPath.row == 4) {
        if ([self.taxesCal.yinhua floatValue] > 0) {
            NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",[Config shareConfig].yinghua];
            cell.sub2Label.text = sub2Str;
        }else{
            NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",@"0"];
            cell.sub2Label.text = sub2Str;
        }
        
    }else if (indexPath.row == 5) {
        NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",self.taxesCal.zonghedijia_cur];
        cell.sub2Label.text = sub2Str;
    }else if (indexPath.row == 6) {
        NSString *sub2Str = [NSString stringWithFormat:@"税率：%@%%",[Config shareConfig].zhongjie];
        cell.sub2Label.text = sub2Str;
    }
    cell.sub1Label.text = self.taxesCal.results[indexPath.row];
    // Configure the cell...
    return cell;

}

//- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
//    return @"";
//}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a story board-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}

 */

@end
